package com.leo.blog.web.link;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dto.LinkExecution;
import com.leo.blog.entity.Bloger;
import com.leo.blog.entity.Link;
import com.leo.blog.enums.LinkStateEnum;
import com.leo.blog.exceptions.LinkOperationException;
import com.leo.blog.service.LinkService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/link")
public class LinkManagementController {
	@Autowired
	private LinkService linkService;
	
	@RequestMapping(value = "/addlink")
	@ResponseBody
	@Transactional
	public Map<String, Object> addLink(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String linkLink = HttpServletRequestUtil.getString(request, "linkLink");
		String linkName = HttpServletRequestUtil.getString(request, "linkName");
		int sort = HttpServletRequestUtil.getInt(request, "sort");
		int status = HttpServletRequestUtil.getInt(request, "status");
		int isApply = HttpServletRequestUtil.getInt(request, "isApply");
		
		Link link = new Link();
		if(linkLink != null && linkLink != "") {
			link.setLinkLink(linkLink);
		}
		if(linkName != null && linkName != "") {
			link.setLinkName(linkName);
		}
		if(sort != 0) {
			link.setSort(sort);
		}
		if(status != 0) {
			link.setStatus(status);
		}
		if(isApply != 0) {
			link.setIsApply(isApply);
		}
	
		//插入数据
		try {
			int effectedNum = linkService.addLink("添加链接", link);
			if(effectedNum > 0) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "添加链接失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", e.toString());
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/deletelink")
	@ResponseBody
	public Map<String, Object> deleteLink(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int linkId = HttpServletRequestUtil.getInt(request, "linkId");
		/**
		 * 手动设置blogerId,后期session
		 */
		Bloger bloger = (Bloger) request.getSession().getAttribute("bloger");
		//bloger.setBlogerId(1);
		//bloger.setHasPermission(1);
		
		if(bloger.getHasPermission() == 1) {
			if(linkId > 0) {
				int effectedNum = linkService.deleteLink("删除链接", linkId);
				if(effectedNum > 0) {
					modelMap.put("success", true);
					modelMap.put("msg", "删除成功");
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "删除失败");
				}
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "删除失败, id为空");
			}
			
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/editlink", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editLink(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		Link link = new Link();
		//获取前台取到的值
		int linkId = HttpServletRequestUtil.getInt(request, "linkId");
		String linkLink = HttpServletRequestUtil.getString(request, "linkLink");
		String linkName = HttpServletRequestUtil.getString(request, "linkName");
		int clickNum = HttpServletRequestUtil.getInt(request, "clickNum");
		int sort = HttpServletRequestUtil.getInt(request, "sort");
		int status = HttpServletRequestUtil.getInt(request, "status");
		int isApply = HttpServletRequestUtil.getInt(request, "isApply");
		
		/**
		 * 手动设置blogerId,后期session
		 */
		Bloger bloger = (Bloger) request.getSession().getAttribute("bloger");
		
		if(bloger.getHasPermission() == 1) {
			//放入link
			link.setLinkId(linkId);
			if(linkLink != null && linkLink != "") {
				link.setLinkLink(linkLink);
			}
			if(linkName != null && linkName != "") {
				link.setLinkName(linkName);
			}
			if(clickNum != 0) {
				link.setClickNum(clickNum);
			}
			if(sort != 0) {
				link.setSort(sort);
			}
			if(status != 0) {
				link.setStatus(status);
			}
			if(isApply != 0) {
				link.setIsApply(isApply);
			}
			try {
				//执行更新操作
				int effectedNum =linkService.editLink("更新链接", link);
				if(effectedNum > 0) {
					modelMap.put("success", true);
					modelMap.put("msg", "更新成功");
				}else {
					modelMap.put("success", false);
					modelMap.put("msg", "更新失败");
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("msg", e.toString());
				return modelMap;
			}		
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "无权限操作");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getlinkbyid")
	@ResponseBody
	public Map<String, Object> getLinkById(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int linkId = HttpServletRequestUtil.getInt(request, "linkId");
		if(linkId > 0) {
			//获取类型信息
			LinkExecution le = linkService.getLinkById(linkId);
			modelMap.put("link", le.getLink());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "empty linkId");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getlinklist")
	@ResponseBody
	public Map<String, Object> getLinkList(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		if((pageIndex > -1) && (pageSize > -1)) {
			//获取传入的需要检索的条件
			//筛选的条件可以进行排列组合
			String linkName = HttpServletRequestUtil.getString(request, "linkName");
			int status = HttpServletRequestUtil.getInt(request, "status");
			int isApply = HttpServletRequestUtil.getInt(request, "isApply");
			//获取组合后的查询条件
			Link linkCondition = compactLinkCondition3Search(linkName, status, isApply);
			//根据查询条件和分页信息获取类型列表,并返回总数
			LinkExecution le = linkService.getLinkList(linkCondition, pageIndex, pageSize);
			modelMap.put("linkList", le.getLinkList());
			modelMap.put("count", le.getCount());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "empty pageSize or pageIndex");
		}
			
		return modelMap;
	}
	
	private Link compactLinkCondition3Search(String linkName, int status, int isApply) {
		Link linkCondition = new Link();
		if(linkName != null) {
			linkCondition.setLinkName(linkName);
		}
		if(status != 0) {
			linkCondition.setStatus(status);
		}
		if(isApply != 0) {
			linkCondition.setIsApply(isApply);
		}
		return linkCondition;
	}

	@RequestMapping(value = "/getalllinklist")
	@ResponseBody
	public Map<String, Object> getAllLinkList(){
		Map<String, Object> modelMap = new HashMap<>();
		LinkExecution le = linkService.getAllLinkList();
		if(le.getState() == LinkStateEnum.SUCCESS.getState()) {
			try {
				modelMap.put("success", true);
				modelMap.put("linkList", le.getLinkList());
			} catch (Exception e) {
				throw new LinkOperationException("获取数据异常");
			}
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "获取数据失败");
		}
		return modelMap;
	}
}
